package com.example.warehousems.event.publisher;

import com.example.warehousems.component.UserInfo;
import com.example.warehousems.config.Constants;
import com.example.warehousems.dao.UserRepository;
import com.example.warehousems.dao.model.JobTicket;
import com.example.warehousems.event.event.JobTicketEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

@Component
public class JobTicketPublisher {
    @Autowired
    private final ApplicationEventPublisher applicationEventPublisher;
    @Autowired
    private UserRepository userRepository;

    public JobTicketPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    public JobTicket publish(JobTicket jobTicket) {
        String title;
        String description;
        Integer arrivalUserId = jobTicket.getProposer().getId();;
        Integer adminId = userRepository.findFirstByRole(0).getId();
        JobTicketEvent event;

        Constants.JOBTICKETSTATUS status = jobTicket.getStatus();
        if (status == Constants.JOBTICKETSTATUS.TO_CHECK) {
            // 用户端消息
            title = "入库工单已创建";
            description = "工单编号为：" + jobTicket.getId()
                    + ",期望入库时间为：" + jobTicket.getEstimateTime()
                    + ",货物有" + jobTicket.getCargoList().size()
                    + "件";
            event = new JobTicketEvent(arrivalUserId, jobTicket, title, description);
            applicationEventPublisher.publishEvent(event);

            // 管理员端消息
            title = "用户创建入库工单，申请入库";
            arrivalUserId = adminId;
            event = new JobTicketEvent(arrivalUserId, jobTicket, title, description);
            applicationEventPublisher.publishEvent(event);
        } else if (status == Constants.JOBTICKETSTATUS.TO_PAY) {
            // 用户端消息
            title = "管理员审核完成";
            description = "工单编号为：" + jobTicket.getId()
                    + ",等待支付";
            event = new JobTicketEvent(arrivalUserId, jobTicket, title, description);
            applicationEventPublisher.publishEvent(event);

            // 管理员端消息
            title = "审核完成";
            description = "工单编号为：" + jobTicket.getId()
                    + ",等待用户支付";
            arrivalUserId = adminId;
            event = new JobTicketEvent(arrivalUserId, jobTicket, title, description);
            applicationEventPublisher.publishEvent(event);
        } else if (status == Constants.JOBTICKETSTATUS.TO_CONFIRM) {
            // 用户端消息
            title = "支付完成";
            description = "工单编号为：" + jobTicket.getId()
                    + ",货物开始入库";
            event = new JobTicketEvent(arrivalUserId, jobTicket, title, description);
            applicationEventPublisher.publishEvent(event);

            // 管理员端消息
            title = "用户支付完成";
            arrivalUserId = adminId;
            event = new JobTicketEvent(arrivalUserId, jobTicket, title, description);
            applicationEventPublisher.publishEvent(event);
        } else if (status == Constants.JOBTICKETSTATUS.DONE) {
            // 用户端消息
            title = "管理员已最终确认";
            description = "工单编号为：" + jobTicket.getId()
                    + ",货物完成入库";
            event = new JobTicketEvent(arrivalUserId, jobTicket, title, description);
            applicationEventPublisher.publishEvent(event);

            // 管理员端消息
            title = "已最终确认";
            arrivalUserId = adminId;
            event = new JobTicketEvent(arrivalUserId, jobTicket, title, description);
            applicationEventPublisher.publishEvent(event);
        } else if (status == Constants.JOBTICKETSTATUS.FAIL) {
            // 用户端消息
            title = "您已取消支付";
            description = "工单编号为：" + jobTicket.getId()
                    + ",工单作废";
            event = new JobTicketEvent(arrivalUserId, jobTicket, title, description);
            applicationEventPublisher.publishEvent(event);

            // 管理员端消息
            title = "用户已取消支付";
            arrivalUserId = adminId;
            event = new JobTicketEvent(arrivalUserId, jobTicket, title, description);
            applicationEventPublisher.publishEvent(event);
        }

        return jobTicket;
    }
}
